package com.liy.cloud.system.service.impl;


import com.liy.cloud.system.dao.SysAccountDao;
import com.liy.cloud.system.dao.SysPasswordHisDao;
import com.liy.cloud.system.entity.SysAccountEntity;
import com.liy.cloud.system.entity.SysPasswordHis;
import com.liy.cloud.system.service.SysAccountService;
import com.liy.cloud.system.service.SysUserRoleService;
import com.liy.cloud.system.vo.SysUpdatePasswordVO;
import com.liy.framework.common.dao.IBaseDao;
import com.liy.framework.common.utils.Tools;
import com.liy.framework.web.spring.service.impl.BaseServiceImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysAccountServiceImpl extends BaseServiceImpl<SysAccountEntity, String> implements SysAccountService {

    @Autowired
    private SysAccountDao accountDao;

    @Autowired
    private SysUserRoleService userRoleService;

    @Autowired
    private SysPasswordHisDao passwordHisDao;


    public IBaseDao<SysAccountEntity, String> getDao() {
        return this.accountDao;
    }

    public SysAccountEntity findByAccount(String account) throws Exception {
        SysAccountEntity accountEntity = new SysAccountEntity();
        accountEntity.setAccount(account);
        return this.accountDao.selectByAccount(accountEntity);
    }

    public void deleteBatch(List<String> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            String id = (String)var2.next();
            this.accountDao.deleteByPrimaryKey(id);
        }

    }

    public List<SysAccountEntity> getAccountArrByIds(List<String> ids) {
        List<SysAccountEntity> entityList = new ArrayList();
        Iterator var3 = ids.iterator();

        while(var3.hasNext()) {
            String id = (String)var3.next();
            SysAccountEntity entity = (SysAccountEntity)this.accountDao.selectByPrimaryKey(id);
            entityList.add(entity);
        }

        return entityList;
    }

    public void delete(String id) throws Exception {
        super.delete(id);
        this.userRoleService.deleteByUserId(id);
    }

    public boolean updatePassword(SysUpdatePasswordVO updatePasswordVO) throws Exception {
        SysAccountEntity account = new SysAccountEntity();
        account.setAccount(updatePasswordVO.getAccount());
        account.setPassword(updatePasswordVO.getNewPass());
        account.setLastUpdateBy(updatePasswordVO.getLastUpdateBy());
        account.setLastUpdateDate(updatePasswordVO.getLastUpdateDate());
        int row = this.accountDao.updatePassword(account);
        if (row <= 0) {
            return false;
        } else {
            SysPasswordHis passwordHis = new SysPasswordHis();
            passwordHis.setId(Tools.getNewUUID());
            passwordHis.setUserId(updatePasswordVO.getAccount());
            passwordHis.setUpdateTime(updatePasswordVO.getLastUpdateDate());
            passwordHis.setOldPass(updatePasswordVO.getOldPass());
            passwordHis.setNewPass(updatePasswordVO.getNewPass());
            passwordHis.setMemo(updatePasswordVO.getMemo());
            //this.passwordHisDao.insertSelective(passwordHis);
            return true;
        }
    }
}

